package com.atguigu.flink.timer;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.TimerService;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * Created by Smexy on 2023/2/28
 *
 *
 *  必须有水印！
 */
public class Demo2_EventTimeTimer
{
    public static void main(String[] args) {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        WatermarkStrategy<WaterSensor> watermarkStrategy = WatermarkStrategy
            //水印会推迟3s
            .<WaterSensor>forMonotonousTimestamps()
            .withTimestampAssigner((e, ts) -> e.getTs());

        //加快水印推进
        env.setParallelism(1);

                env
                   .socketTextStream("hadoop103", 8888)
                   .map(new WaterSensorMapFunction())
                   .assignTimestampsAndWatermarks(watermarkStrategy)
                   .keyBy(WaterSensor::getId)
                   .process(new KeyedProcessFunction<String, WaterSensor, String>()
                   {

                       @Override
                       public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {

                                out.collect(ctx.getCurrentKey() +"  定时器报警了"+timestamp);
                                time = 0;

                       }

                       private long time = 0;

                       /*
                                                                          窗口.process():  窗口关闭时，只执行一次。

                                                                          无窗口.process():  来一条数据，就执行一次
                       */
                       @Override
                       public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {

                           TimerService timerService = ctx.timerService();

                           if (value.getVc() > 30){
                               //根据当前数据的事件时间(ctx.timestamp())，制定定时器
                               time = ctx.timestamp() + 5000;
                               timerService.registerEventTimeTimer(time);
                               System.out.println(value + "到达，制定定时器:"+time);
                           }else if (value.getVc() <= 10){

                               timerService.deleteEventTimeTimer(time);
                               System.out.println(value + "到达，删除定时器:"+time);

                           }

                       }
                   })
                   .print();


                try {
                            env.execute();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

    }

}
